#include "scenes/GameScene.h"

#include <exception>
#include <algorithm>
#include <stdexcept>

#include "objects/GameObject.h"

GameScene::GameScene( GameApplication& theApp, SceneGame& theScene )
   : App( theApp ), Scene( theScene )
{

}

void GameScene::Initialize()
{

}

void GameScene::Terminate()
{

}

void GameScene::Step()
{
	for ( std::list< GameObject* >::iterator it = objects.begin(); it != objects.end(); ++it )
	{
		( * it )->StepEvent();
	}

	SortObjectsByDepth();
}

void GameScene::Step( gdn::Window& window )
{
	throw std::runtime_error( "'void GameScene::Step( gdn::Window& window )' should never be called!" );
}

void GameScene::Step( gdn::Event& event )
{
	for ( std::list< GameObject* >::iterator it = objects.begin(); it != objects.end(); ++it )
	{
		( * it )->StepEvent( event );
	}

	if ( event.type == gdn::Event::WindowClosed )
	{
		App.Close();
	}
}

void GameScene::Draw( gdn::Window& window )
{
	for ( std::list< GameObject* >::iterator it = objects.begin(); it != objects.end(); ++it )
	{
		( * it )->Draw( window );
	}
}

void GameScene::SortObjectsByDepth()
{
	objects.sort( GameScene::DepthSorter );
}

bool GameScene::DepthSorter( GameObject* obj1, GameObject* obj2 )
{
	return ( obj1->GetDepth() < obj2->GetDepth() );
}
